﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WarehouseManagement.BusinessLogic.BC
{
    public class RevenueBC : BaseBC
    {
        private MerchandiseBC _merchandiseBC;

        public RevenueBC()
        {
            _merchandiseBC = new MerchandiseBC();
        }

        //public void UpdateMonthlyRevenue(int merchandiseId, int month, int year, double exportQuantity, decimal exportValue, decimal profit)
        //{
        //    if (!RevenueItemExists(merchandiseId, month, year))
        //    {
        //        Context.MonthlyRevenues.InsertOnSubmit(new DataAccess.MonthlyRevenue()
        //        {
        //            Id = Guid.NewGuid(),
        //            MerchandiseId  = merchandiseId,
        //            Month = month,
        //            Year = year,
        //            TotalProfit = profit,
        //            TotalQuantity = exportQuantity,
        //            TotalRevenue = exportValue

        //        });
        //    }
        //    else
        //    {
        //        var existingItem = Context.MonthlyRevenues.SingleOrDefault(e => e.MerchandiseId == merchandiseId
        //            && (e.Month == month && e.Year == year));
        //        existingItem.TotalQuantity += exportQuantity;
        //        existingItem.TotalRevenue += exportValue;
        //        existingItem.TotalProfit += profit;

        //    }
        //    SubmitChanges();
        //}

        //private bool RevenueItemExists(int merchandiseId, int month, int year)
        //{
        //    return Context.MonthlyRevenues.Any(e => e.MerchandiseId == merchandiseId
        //         && (e.Month == month && e.Year == year));
        //}

        public void GetMonthlyImportExportValue(int month, int year,
            out decimal importVal, out decimal exportVal,
            out decimal profitVal)
        {
            importVal = exportVal = profitVal = 0;


            var list = Context.MonthlyImportExports.Where(e => e.Month == month && e.Year == year);
            if (list.Count() > 0)
            {
                importVal = list.Sum(e => e.ImportValue);
                exportVal = list.Sum(e => e.ExportValue);
                profitVal = list.Sum(e => e.Profit);
            }

        }

        public void GetYearlyImportExportValue(int year,
            out decimal importVal, out decimal exportVal,
            out decimal profitVal)
        {
            importVal = exportVal = profitVal = 0;


            var list = Context.MonthlyImportExports.Where(e => e.Year == year);
            if (list.Count() > 0)
            {
                importVal = list.Sum(e => e.ImportValue);
                exportVal = list.Sum(e => e.ExportValue);
                profitVal = list.Sum(e => e.Profit);
            }
        }
    }
}
